package com.ce.webframework.sec.service.impl;

import java.util.List;

import com.ce.webframework.sec.model.SecDragSourceDO;
import com.ce.webframework.sec.model.SecDragSourceTempDO;
import com.ce.webframework.sec.model.SecDragTargetDO;
import com.ce.webframework.sec.service.ISecDragSourceService;
import com.ce.webframework.sec.dao.ISecDragSourceDao;
import com.ce.webframework.sec.dao.ISecDragSourceTempDao;
import com.ce.webframework.sec.dao.ISecDragTargetDao;
import com.sitechasia.webx.core.service.impl.BaseService;

/**
 * 拖动源manager
 *
 */
public class SecDragSourceServiceImpl extends BaseService implements ISecDragSourceService {

	private ISecDragSourceDao secDragSourceDao;
	private ISecDragSourceTempDao secDragSourceTempDao;
	private ISecDragTargetDao secDragTargetDao;

	/**
	 * 设置拖动源dao
	 * @param secDragSourceDao 拖动源dao
	 */
	public void setSecDragSourceDao(ISecDragSourceDao secDragSourceDao) {
		this.secDragSourceDao = secDragSourceDao;
	}

	/**
	 * 设置拖动源模板dao
	 * @param secDragSourceTempDao 拖动源模板dao
	 */
	public void setSecDragSourceTempDao(ISecDragSourceTempDao secDragSourceTempDao) {
		this.secDragSourceTempDao = secDragSourceTempDao;
	}

	/**
	 * 设置容器dao
	 * @param secDragTargetDao 拖动源模板dao
	 */
	public void setSecDragTargetDao(ISecDragTargetDao secDragTargetDao) {
		this.secDragTargetDao = secDragTargetDao;
	}

	/**
	 * 功能描述:返回当前用户选择的模块
	 * @param userId  用户标识
	 * @param funcEntityIds 权限标识 集合
	 * @param epId  企业标识
	 * @return SecDragSourceDO 集合
	 */
	public List<SecDragSourceDO> getUseModels(Long userId,List<String> funcEntityIds, Long epId){

		return secDragSourceDao.getDragSources(userId, funcEntityIds, epId);

	}

	/**
	 * 功能描述:应用模块
	 * @param userId  用户标识
	 * @param dragSourceTempId  拖动源模板标识
	 * @param dragTargetId  容器标识
	 * @param orderNum  顺序号
	 * @param epId  企业标识
	 * @return 无
	 */
	public void applyUse(Long userId, String dragSourceTempId
			, String dragTargetId, int orderNum, Long epId){

		boolean isExist = secDragSourceDao.isExist(userId, dragSourceTempId, epId);
		//如果不存在才增加
		if (!isExist){
			SecDragSourceTempDO sdst = secDragSourceTempDao.findById(Long.parseLong(dragSourceTempId));
			SecDragTargetDO sdt = secDragTargetDao.getDragTargetByName(dragTargetId, epId);
			secDragSourceDao.addDragSource(userId, sdt, sdst, orderNum);//增加dao方法需要改
		}

	}

	/**
	 * 功能描述:更新模块位置
	 * @param dragSourceTempId  拖动源模板标识
	 * @param orderNum  顺序号
	 * @param userId  用户标识
	 * @param epId  企业标识
	 * @return 无
	 */
	public void updateModelSite(String dragSourceTempId
			, String dragTargetId, int orderNum, Long userId, Long epId){

		if(userId != null && !userId.equals("")){

			SecDragSourceDO sdsdo = this.secDragSourceDao
				.searchDragSource(userId, dragSourceTempId, epId);
			System.out.println("dragTargetId:" + dragTargetId + "epId" + epId);
//			SecDragTargetDO dragTargetDO = (secDragTargetDao.findBy("name", dragTargetId)).get(0);
			SecDragTargetDO dragTargetDO = secDragTargetDao.getDragTargetByName(dragTargetId, epId);
			System.out.println("secDragTargetDao:" + dragTargetDO.getType());
			SecDragSourceTempDO dragSourceTempDO = secDragSourceTempDao.findById(Long.parseLong(dragSourceTempId));

			secDragSourceDao.updateDragSource(sdsdo.getDragSourceId(), dragTargetDO
					, dragSourceTempDO, orderNum, userId);

		}

	}

	/**
	 * 功能描述:取消模块的应用
	 * @param userId  用户标识
	 * @param dragSourceTempId  拖动源模板标识
	 * @param epId  企业标识
	 * @return 无
	 */
	public void appleUnuse(Long userId, String dragSourceTempId, Long epId){

		if (userId != null && !userId.equals("")){

			SecDragSourceDO sdsdo = this.secDragSourceDao
				.searchDragSource(userId, dragSourceTempId, epId);

			secDragSourceDao.deleteDragSource(userId, sdsdo.getDragSourceId());
		}

	}




}
